package com.hmdp.utils;

import cn.hutool.core.lang.UUID;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;

import java.util.Collections;
import java.util.concurrent.TimeUnit;

// 分布式锁
public class SimpleRedisLock implements ILock{
    private String name;
    private StringRedisTemplate stringRedisTemplate;
    private static final String KEY_PREFIX="lock:";
    private static final String ID_PREFIX= UUID.randomUUID().toString(true)+"-";
    private static final DefaultRedisScript<Long> UNLOCK_SCRIPT;
    static {
        UNLOCK_SCRIPT=new DefaultRedisScript<>();
        UNLOCK_SCRIPT.setLocation(new ClassPathResource("unlock.lua"));
        UNLOCK_SCRIPT.setResultType(Long.class);
    }

    public SimpleRedisLock(String name, StringRedisTemplate stringRedisTemplate) {
        this.name = name;
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Override
    public boolean tryLock(long timeoutSec) {
        // 此方法不行，容易出现两个服务器线程ID一样的情况，所以要在线程ID前加入ID_PREFIX
        // String threadId=Thread.currentThread().getId();
        //
        // Boolean success = stringRedisTemplate.opsForValue()
        //         .setIfAbsent(KEY_PREFIX + name, threadId+"" , timeoutSec, TimeUnit.SECONDS);
        
        
        String threadId=ID_PREFIX+Thread.currentThread().getId();

        Boolean success = stringRedisTemplate.opsForValue()
                .setIfAbsent(KEY_PREFIX + name, threadId , timeoutSec, TimeUnit.SECONDS);

        return Boolean.TRUE.equals(success);
    }

    // lua脚本实现原子性释放锁
    @Override
    public void unlock(){
        stringRedisTemplate.execute(UNLOCK_SCRIPT, Collections.singletonList(KEY_PREFIX + name),ID_PREFIX+Thread.currentThread().getId());
    }
    
    
    // 不能实现原子性
    // @Override
    // public void unlock() {
    //     String threadId=ID_PREFIX+Thread.currentThread().getId();
    //    
    //     String id=stringRedisTemplate.opsForValue().get(KEY_PREFIX + name);
    //    
    //     // 两个线程值相等就释放锁
    //     if(threadId.equals(id)){
    //         如果第一个jvm的线程1执行到这个代码，jvm突然阻塞，同时另一个jvm的线程2执行，此时线程1突然释放锁
    //         就把线程2的锁给释放掉了
    //         stringRedisTemplate.delete(KEY_PREFIX + name);
    //     }
    //    
    //     stringRedisTemplate.delete(KEY_PREFIX+name);
    // }
}
